package com.hui.web.servlet;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SimplePropertyPreFilter;
import com.hui.base.BaseServlet;
import com.hui.pojo.Course;
import com.hui.service.CourseService;
import com.hui.service.impl.CourseServiceImpl;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Auther: 李昭辉
 * @Date: 2021/07/11/18:26
 * @Description:
 */
@WebServlet("/course")
public class CourseServlet extends BaseServlet {
    private CourseService courseService = new CourseServiceImpl();

    // 查询课程列表信息
    public void findCourseList(HttpServletRequest req, HttpServletResponse resp) {
        try {
            // 获取课程信息
            List<Course> courseList = courseService.getCourseList();
            // SimplePropertyPreFilter 指定要转换的JSON字段
            SimplePropertyPreFilter filter = new SimplePropertyPreFilter(Course.class, "id", "course_name", "price", "sort_num", "status");
            // 将课程信息转成JSON字符串格式,并传递一个过滤器
            String result = JSON.toJSONString(courseList, filter);
            // 将结果写出去
            PrintWriter writer = resp.getWriter();
            writer.write(result);
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 根据条件查询课程信息
    public void findByCourseNameAndStatus(HttpServletRequest req, HttpServletResponse resp) {
        String courseName = req.getParameter("course_name");
        String status = req.getParameter("status");
        // 获取查询结果
        List<Course> courseList = courseService.getCourseByCourseNameAndStatus(courseName, status);
        // 将结果解析成JSON字符串格式
        SimplePropertyPreFilter filter = new SimplePropertyPreFilter(Course.class, "id", "course_name", "price", "sort_num", "status");
        String res = JSON.toJSONString(courseList, filter);
        // 将结果写出去
        try {
            PrintWriter writer = resp.getWriter();
            writer.write(res);
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 根据ID获取课程营销信息
    public void findCourseById(HttpServletRequest req, HttpServletResponse resp) {
        try {
            String id = req.getParameter("id");
            // 获取结果
            Course course = courseService.getCourseSaleInfoById(Integer.parseInt(id));
            // 转换成JSON格式
            SimplePropertyPreFilter filter = new SimplePropertyPreFilter(Course.class,
                    "id", "course_name", "brief", "teacher_name", "teacher_info",
                    "preview_first_field", "preview_second_field", "discounts", "price", "price_tag",
                    "share_image_title", "share_title", "share_description", "course_description",
                    "course_img_url", "status"
            );
            String res = JSON.toJSONString(course, filter);
            // 写出去
            PrintWriter writer = resp.getWriter();
            writer.write(res);
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    // 修改课程状态
    public void updateCourseStatus(HttpServletRequest req, HttpServletResponse resp) {
        try {
            String id = req.getParameter("id");
            int status = courseService.updateCourseStatusById(Integer.parseInt(id));
            // 以 {"status":0} 形式返回给前端
            JSONObject res = new JSONObject();
            res.put("status", status);
            // 将结果写出去
            PrintWriter writer = resp.getWriter();
            writer.write(res.toString());
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
